package com.gzc.just.play.last.war.gateserver.connection;

import com.gzc.just.play.last.war.common.connection.NacosAwareConnector;
import com.gzc.just.play.last.war.common.connection.ServerConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import java.util.HashMap;
import java.util.Map;

/**
 * GateServer connector
 * Responsible for establishing and maintaining connections from GateServer to other servers
 * 支持Nacos服务发现和静态配置两种模式
 */
@Component
public class GateServerConnector extends NacosAwareConnector {
    
    private static final Logger log = LoggerFactory.getLogger(GateServerConnector.class);
    
    @Value("${central.server.host:127.0.0.1}")
    private String centralServerHost;
    
    @Value("${central.server.port:6001}")
    private int centralServerPort;
    
    @Value("${scene.server.host:127.0.0.1}")
    private String sceneServerHost;
    
    @Value("${scene.server.port:50001}")
    private int sceneServerPort;
    
    @Value("${login.server.host:127.0.0.1}")
    private String loginServerHost;
    
    @Value("${login.server.port:30001}")
    private int loginServerPort;
    
    @PostConstruct
    public void init() {
        log.info("Initializing GateServer connections...");
        initializeConnections();
        log.info("GateServer connections initialization completed");
    }
    
    @Override
    protected Map<String, ServerConnectionManager.ServerType> getDependencies() {
        Map<String, ServerConnectionManager.ServerType> dependencies = new HashMap<>();
        // Nacos服务名与ServerType的映射
        dependencies.put("central-server", ServerConnectionManager.ServerType.CENTRAL_SERVER);
        dependencies.put("scene-server", ServerConnectionManager.ServerType.SCENE_SERVER);
        dependencies.put("login-server", ServerConnectionManager.ServerType.LOGIN_SERVER);
        return dependencies;
    }
    
    @Override
    protected void initializeWithStaticConfig() {
        log.info("Initializing with static configuration (Nacos disabled)");
        
        // Register server configuration
        connectionManager.registerServerConfig(
                ServerConnectionManager.ServerType.CENTRAL_SERVER,
                new ServerConnectionManager.ServerConfig(centralServerHost, centralServerPort)
        );
        
        connectionManager.registerServerConfig(
                ServerConnectionManager.ServerType.SCENE_SERVER,
                new ServerConnectionManager.ServerConfig(sceneServerHost, sceneServerPort)
        );
        
        connectionManager.registerServerConfig(
                ServerConnectionManager.ServerType.LOGIN_SERVER,
                new ServerConnectionManager.ServerConfig(loginServerHost, loginServerPort)
        );
        
        log.info("GateServer connections configured");
    }
    
    @PreDestroy
    public void shutdown() {
        log.info("Shutting down GateServer connections...");
        shutdownConnections();
        log.info("GateServer connections shut down");
    }
    
    /**
     * Get the connection manager instance
     * @return ServerConnectionManager instance
     */
    public ServerConnectionManager getConnectionManager() {
        return connectionManager;
    }
}
